import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import NProgress from "nprogress"
import 'nprogress/nprogress.css'
import { Message } from 'element-ui'
Vue.use(VueRouter)

const Layout = () => import("components/common/layout/Layout")
const Login = () => import("views/login/Login")
const Home = () => import("views/home/Home")
const List = () => import("views/customer/List")
const Report = () => import("views/customer/Report")
const Cillegal = () => import("views/customer/Illegal")
const Record = () => import("views/customer/Record")
const Evaluation = () => import("views/customer/Evaluation")
const routes = [
  {
    path: '',
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: '/home',
    name: "Home",
    component: Layout,
    children: [
      {
        path: "/home",
        name: 'Home',
        component: Home,
        meta: {
          title: "首页",
          isTabsPage: true,
        }
      },
      {
        path: "/customer-list",
        name: 'List',
        component: List,
        meta: {
          title: "用户列表",
          isTabsPage: true,
        }
      },
      {
        path: "/customer-report",
        name: 'Report',
        component: Report,
        meta: {
          title: "举报处理",
          isTabsPage: true,
        }
      },
      {
        path: "/customer-illegal",
        name: "Cillegal",
        component: Cillegal,
        meta: {
          title: "违规处理",
          isTabsPage: true,
        }
      },
      {
        path: "/customer-record",
        name: "Record",
        component: Record,
        meta: {
          title: "充值记录",
          isTabsPage: true,
        }
      },
      {
        path: "/customer-evaluation",
        name: "Evaluation",
        component: Evaluation,
        meta: {
          title: "用户评价",
          isTabsPage: true,
        }
      }
    ]
  }

]

const router = new VueRouter({
  routes,
  mode: 'history'
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  // if (to.meta.title) {
  //   document.title = to.meta.title + ' | 1024'
  // }
  if (to.path !== '/login') {
    // 如果还未登录，进行拦截
    if (!store.getters.isLogin) {
      Message.error('您还未登录,请先登录!')
      return next('/login')
    }
    //     // 权限控制
    //     if (to.meta.requireAuth && !store.getters.isAdmin) {
    //         Message.error('您无权限访问')
    //         return next('/401')
    //     }

    // 判断是否需要添加到tabs标签
    if (to.meta.isTabsPage) {
      const tabsItem = {
        name: to.name,
        title: to.meta.title,
        path: to.path,
      }
      store.commit('ADD_TABS_VALUE', tabsItem)
    }
  }
  next()
})
router.afterEach(() => {
  NProgress.done()
})
export default router
